﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PublicCommon;
using BLL;
using WinFormQuickQureyInterface;
using ORMapping;
using MForm;

namespace CardMoudle
{
    public partial class CardForm : MdiForm,IQuickQueryInterface
    {
        private TBLCardTypeBLL tctb = new TBLCardTypeBLL();
        public CardForm(Parent im) : base(im)
        {
            InitializeComponent();
            HYDataGridView.Helper.InitDataGridViewColumn(String.Format("{0}_{1}", this.Name, this.dgvCard.Name), ref dgvCard);
            dgvCard.AutoGenerateColumns = false;
            IList<TBLCardType> ilSource = tctb.GetAllCardTypes();
            ilSource.Insert(0, new TBLCardType { CardType = string.Empty, CardName = string.Empty });
            CardType.DataSource = ilSource;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            Query();
        }

        public override void Query()
        {
            CardBLL cdb = new CardBLL();
            dgvCard.Columns.Clear();
            HYDataGridView.Helper.InitDataGridViewColumn(String.Format("{0}_{1}", this.Name, this.dgvCard.Name), ref dgvCard);
            dgvCard.DataSource = cdb.GetDataGridViewShowCards(this.Name,this.Controls);
            HYDataGridView.Helper.SetUsefulLifeOverWarning(ref this.dgvCard);
        }


        private void csmModify_Click(object sender, EventArgs e)
        {
            DataRow drCardInfo = HYDataGridView.Helper.GetDataGridViewSelectedRow(this,dgvCard);
            if (null == drCardInfo)
            {
                return;
            }
            //传递初始化数据
            ModifyCardInfoForm frm = new ModifyCardInfoForm(ref drCardInfo);
            frm.IsNewInfo = false;
            frm.Text = "修改攀岩卡信息";
            frm.ShowDialog();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewCardInfo();
        }

        private void csmNew_Click(object sender, EventArgs e)
        {
            NewCardInfo();
        }

        private void NewCardInfo()
        {
            ModifyCardInfoForm frm = new ModifyCardInfoForm();
            frm.Text = "新建攀岩卡信息";
            frm.ShowDialog();
        }

        #region IQuickQueryInterface 成员

        object IQuickQueryInterface.GetQuickQueryCondition()
        {
            DataRow drCard = HYDataGridView.Helper.GetDataGridViewSelectedRow(this, dgvCard);
            if (null == drCard)
            {
                return null;
            }
            ORMapping.Card cd = new ORMapping.Card();
            NHibernateHelper.Convert.DataRowConvertToObject(cd, drCard);
            return cd;
        }

        void IQuickQueryInterface.QuickQuery(object oQuickQueryCondition)
        {
            Control.ControlCollection ccc = this.Controls;
            Functions.SetQueryControlText(ref ccc, oQuickQueryCondition);
            Query();
        }

        #endregion

        private void dgvCard_DoubleClick(object sender, EventArgs e)
        {
            ShowMemberInfo();
        }

        private void csmMemberInfo_Click(object sender, EventArgs e)
        {
            ShowMemberInfo();
        }

        private void ShowMemberInfo()
        {
            DataRow drTarget = HYDataGridView.Helper.GetDataGridViewSelectedRow(this, dgvCard);
            if (null == drTarget)
            {
                MessageBox.Show("没有选择需要显示的行");
                return;
            }
            ORMapping.Card cd = new ORMapping.Card();
            NHibernateHelper.Convert.DataRowConvertToObject(cd, drTarget);
            CardMemberForm cmf = new CardMemberForm(cd);
            cmf.ShowDialog();
        }

        private void csmHistory_Click(object sender, EventArgs e)
        {
            DataRow drTarget = HYDataGridView.Helper.GetDataGridViewSelectedRow(this, dgvCard);
            if (null == drTarget)
            {
                MessageBox.Show("没有选择需要显示的行");
                return;
            }
            ORMapping.Card cd = new ORMapping.Card();
            NHibernateHelper.Convert.DataRowConvertToObject(cd, drTarget);
            CardHistoryForm chf = new CardHistoryForm(cd);
            chf.ShowDialog();
        }
    }
}